﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET QUOTED_IDENTIFIER ON

GO


CREATE TABLE [dbo].[bht_ApartmentGroup](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](400) NOT NULL,
	[NumberOfPeople] [int] NOT NULL,
	[DefaultLatitude] [numeric](18, 12) NOT NULL,
	[DefaultLongitude] [numeric](18, 12) NOT NULL,
	[DefaultAreaSize] [float] NOT NULL,
	[DefaultLocationName] [nvarchar](400) NULL,
 CONSTRAINT [PK_bhp_ApartmentGroup] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
) ON [PRIMARY],
CONSTRAINT [IX_bhp_ApartmentGroup] UNIQUE NONCLUSTERED 
(
	[Name] ASC
) ON [PRIMARY]
) ON [PRIMARY]

GO


CREATE PROCEDURE [dbo].[bhp_ApartmentGroupSave]
(
	@Id						INT,
	@Name					NVARCHAR(400),
	@NumberOfPeople			INT,
	@DefaultLatitude		NUMERIC(18, 12),
	@DefaultLongitude		NUMERIC(18, 12),
	@DefaultAreaSize		FLOAT,
	@DefaultLocationName	NVARCHAR(400)
)
AS
BEGIN

	IF @Id = 0 OR NOT EXISTS (SELECT 1 FROM bht_ApartmentGroup WHERE Id = @Id)
	BEGIN
		INSERT INTO bht_ApartmentGroup (Name, NumberOfPeople, DefaultLatitude, DefaultLongitude, DefaultAreaSize, DefaultLocationName)
			VALUES (@Name, @NumberOfPeople, @DefaultLatitude, @DefaultLongitude, @DefaultAreaSize, @DefaultLocationName)
	END
	ELSE
	BEGIN
		UPDATE
			bht_ApartmentGroup
		SET
			Name					= @Name,
			NumberOfPeople			= @NumberOfPeople,
			DefaultAreaSize			= @DefaultAreaSize,
			DefaultLatitude			= @DefaultLatitude,
			DefaultLocationName		= @DefaultLocationName,
			DefaultLongitude		= @DefaultLongitude
		WHERE
			Id						= @Id
	END
	
	SELECT SCOPE_IDENTITY()
END
	
GO


CREATE PROCEDURE [dbo].[bhp_ApartmentGroupGet]
(
	@Id						INT
)
AS
BEGIN

	SELECT 
		[Id],
		[Name],
		[NumberOfPeople],
		[DefaultLatitude] as [DefaultLocalizationLatitude],
		[DefaultLongitude] as [DefaultLocalizationLongitude],
		[DefaultAreaSize] as [AreaSize],
		[DefaultLocationName]
	FROM 
		[bht_ApartmentGroup]
	WHERE
		[Id]	= @Id



END
	
	
GO
